// 云函数入口文件
const cloud = require('wx-server-sdk')
var STS = require('qcloud-cos-sts');
cloud.init()
//  主账号ID 100013014413 用户名 yjs 登录密码 po}MM:rMm60jTPUf%6Ga?)vg5v|O**{
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  return new Promise((resolve, reject)=>{
    var policy = {
      'version': '2.0',
      'statement': [{
        'action': [
          // 简单上传
          'name/cos:PutObject',
          'name/cos:PostObject',
          'name/cos:DeleteObject'
          // // 分片上传
          // 'name/cos:InitiateMultipartUpload',
          // 'name/cos:ListMultipartUploads',
          // 'name/cos:ListParts',
          // 'name/cos:UploadPart',
          // 'name/cos:CompleteMultipartUpload'
        ],
        'effect': 'allow',
        'principal': { 'qcs': ['*'] },
        'resource': [
          `qcs::cos:ap-chongqing:uid/1301301505:${event.bucket}/*`,
        ],
      }],
    };
    STS.getCredential({
      secretId: 'AKIDlJa3GZNOedS0HJJYlUxrVtjJfkhOM0uN',
      secretKey: 'Xp0bLq4JK4h5DEBsTpwtc6SaRImAHL9i',
      policy: policy,
      durationSeconds: 600,
      // proxy: '',
      }, function (err, credential) {
        if (err){
          reject(err)
        }else{
          resolve(credential);
        }
        // console.log(err || credential);
    });
  });
}
